package com.salt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.salt.entity.IdleAffiliateMaterial;
import com.salt.mapper.IdleAffiliateMaterialMapper;
import com.salt.service.IdleAffiliateMaterialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 闲鱼联盟物料信息服务实现类
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class IdleAffiliateMaterialServiceImpl implements IdleAffiliateMaterialService {

    private final IdleAffiliateMaterialMapper materialMapper;
    
    @Override
    public Page<IdleAffiliateMaterial> page(Page<IdleAffiliateMaterial> page, String categoryName, String status) {
        // 构建查询条件
        LambdaQueryWrapper<IdleAffiliateMaterial> wrapper = new LambdaQueryWrapper<>();
        
        // 按商品分类查询
        if (StringUtils.hasText(categoryName)) {
            wrapper.eq(IdleAffiliateMaterial::getCategoryName, categoryName);
        }
        
        // 按商品状态查询
        if (StringUtils.hasText(status)) {
            wrapper.eq(IdleAffiliateMaterial::getStatus, status);
        }
        // 只查询在线的物料
        wrapper.eq(IdleAffiliateMaterial::getIsOnline, Boolean.TRUE);
        // 按创建时间倒序排序
        wrapper.orderByDesc(IdleAffiliateMaterial::getCreateTime);
        
        // 执行分页查询
        return materialMapper.selectPage(page, wrapper);
    }

    @Override
    public IdleAffiliateMaterial getByItemId(String itemId) {
        if (!StringUtils.hasText(itemId)) {
            return null;
        }
        
        // 构建查询条件
        LambdaQueryWrapper<IdleAffiliateMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdleAffiliateMaterial::getItemId, itemId);
        wrapper.eq(IdleAffiliateMaterial::getIsOnline, Boolean.TRUE);
        
        // 执行查询
        return materialMapper.selectOne(wrapper);
    }

    @Override
    public IdleAffiliateMaterial getById(String itemId) {
        return materialMapper.selectById(itemId);
    }
} 